package com.shop.utils;

/**
 * 
 * @Title EncodeUtils
 *        </p>
 * @Project runlin-framework
 * @Package com.runlin.framework.encode
 * @author Kevin Lv
 * @date 2016年9月5日 下午1:07:55
 * @version V1.0
 */
public class EncodeUtils {

    private static final char C1 = 'a';

    private static final char C2 = 'z';

    private static final char C3 = 'A';

    private static final char C4 = 'Z';

    /**
     * 
     * @Title: getString @Description: 移位加解密 @param @param str @param @param key @param @return
     * 设定文件 @return String 返回类型 @throws
     */
    public static String getString(String str, int key) {
        key %= 26;
        if (key == 0) {
            return str;
        }
        char[] chars = str.toCharArray();
        for (int i = chars.length - 1; i >= 0; i--) {
            if (chars[i] >= C3 && chars[i] <= C4) {
                chars[i] += key;
                if (chars[i] > C4) {
                    chars[i] = (char) (chars[i] - C4 + C3 - 1);
                } else if (chars[i] < C3) {
                    chars[i] = (char) (C4 - (C3 - chars[i]) + 1);
                }
            } else if (chars[i] >= C1 && chars[i] <= C2) {
                chars[i] += key;
                if (chars[i] > C2) {
                    chars[i] = (char) (chars[i] - C2 + C1 - 1);
                } else if (chars[i] < C1) {
                    chars[i] = (char) (C2 - (C1 - chars[i]) + 1);
                }
            }
        }
        return new String(chars);
    }

    /*
     * public static void main(String[] args) { String str = "P_AP,P_BASE,P_MB"; // 加密 String str2 =
     * getString(str, 1); System.out.println(str2); // 解密 用 -KEY System.out.println(getString( str2,
     * -1)); }
     */

}
